/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package library.shell;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.List;
import javax.swing.JOptionPane;
import library.book.Book;
import library.control.ControlProvider;
import library.control.IControlBook;

/**
 *
 * @author MonStar
 */
public class JFrameAddBook extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public JFrameAddBook() {
        initComponents();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = getSize();
        setLocation(new Point((screenSize.width - frameSize.width) / 2,
                (screenSize.height - frameSize.width) / 2));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField_NameBook = new javax.swing.JTextField();
        jLabel_NameBook = new javax.swing.JLabel();
        jLabel_Autor = new javax.swing.JLabel();
        jTextField_Autor = new javax.swing.JTextField();
        jLabel_Year = new javax.swing.JLabel();
        jTextField_YearBook = new javax.swing.JTextField();
        jLabel_Url = new javax.swing.JLabel();
        jTextField_Url = new javax.swing.JTextField();
        jButton_Add = new javax.swing.JButton();
        jComboBox_SelectStorage = new javax.swing.JComboBox();

        setTitle("Добавление книги");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel_NameBook.setText("Название:");

        jLabel_Autor.setText("Автор:");

        jLabel_Year.setText("Год:");

        jLabel_Url.setText("Расположение:");

        jTextField_Url.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField_UrlActionPerformed(evt);
            }
        });

        jButton_Add.setText("Добавить");
        jButton_Add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_AddActionPerformed(evt);
            }
        });

        jComboBox_SelectStorage.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "static", "file", "data base"}));
        jComboBox_SelectStorage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_SelectStorageActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel_NameBook)
                            .addComponent(jLabel_Autor)
                            .addComponent(jLabel_Year)
                            .addComponent(jLabel_Url))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField_NameBook)
                            .addComponent(jTextField_Autor)
                            .addComponent(jTextField_YearBook)
                            .addComponent(jTextField_Url, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                            .addComponent(jButton_Add, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jComboBox_SelectStorage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jComboBox_SelectStorage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_NameBook)
                    .addComponent(jTextField_NameBook, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Autor)
                    .addComponent(jTextField_Autor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Year)
                    .addComponent(jTextField_YearBook, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField_Url, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_Url))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton_Add)
                .addContainerGap())
        );

        jTextField_YearBook.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent e) {
                char a = e.getKeyChar();
                if (!Character.isDigit(a)
                    && (a != '.')
                    && (a != '\b')) {
                    e.consume();
                }
            }
        });
        jComboBox_SelectStorage.getAccessibleContext().setAccessibleDescription("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox_SelectStorageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_SelectStorageActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox_SelectStorageActionPerformed

    private void jButton_AddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_AddActionPerformed
        // TODO add your handling code here:
        String storage = jComboBox_SelectStorage.getSelectedItem().toString();
        Book book = new Book();
        book.setId();
        book.setName(jTextField_NameBook.getText());
        book.setAutor(jTextField_Autor.getText());
        book.setYear(Integer.parseInt(jTextField_YearBook.getText()));
        book.setLink(jTextField_Url.getText());
        List<IControlBook> listControlBook = ControlProvider.getControl().getListControlBook();
        for (IControlBook controlBook : listControlBook) {
            if (controlBook.getName().equals(storage)){
                controlBook.addBook(book);
            }
        }
        JOptionPane.showMessageDialog(null, "Книга успешно добавлена! Обновите список пожалуйста.", null, JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jButton_AddActionPerformed

    
    private void jTextField_UrlActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField_UrlActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField_UrlActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main() {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFrameAddBook.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFrameAddBook.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFrameAddBook.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrameAddBook.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JFrameAddBook().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Add;
    private javax.swing.JComboBox jComboBox_SelectStorage;
    private javax.swing.JLabel jLabel_Autor;
    private javax.swing.JLabel jLabel_NameBook;
    private javax.swing.JLabel jLabel_Url;
    private javax.swing.JLabel jLabel_Year;
    private javax.swing.JTextField jTextField_Autor;
    private javax.swing.JTextField jTextField_NameBook;
    private javax.swing.JTextField jTextField_Url;
    private javax.swing.JTextField jTextField_YearBook;
    // End of variables declaration//GEN-END:variables
}
